package com.ljkj.cordial.chat.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.bean.Canstants;
import com.nic.view.widget.ContainsEmojiEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索好友
 */
public class SearchFriendActivity extends BaseActivity {


    @BindView(R.id.et_search)
    ContainsEmojiEditText etSearch;
    @BindView(R.id.ll_f_panel)
    LinearLayout llFPanel;
    @BindView(R.id.tv_f_content)
    TextView tvFContent;
    @BindView(R.id.ll_g_panel)
    LinearLayout llGPanel;
    @BindView(R.id.tv_g_content)
    TextView tvGContent;

    int searchType;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_search_friend;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("查找");
        searchType = getIntent().getIntExtra(Canstants.KEY_SEARCH_TYPE, Canstants.SEARCH_TYPE_FRIEND_GROUP);
        setShowDefNetDialog(false);
        if (searchType == Canstants.SEARCH_TYPE_FRIEND_GROUP) {
            etSearch.setHint("ID搜索好友或群组");
        } else if (searchType == Canstants.SEARCH_TYPE_FRIEND) {
            etSearch.setHint("ID搜索好友");
        } else if (searchType == Canstants.SEARCH_TYPE_GROUP) {
            etSearch.setHint("ID搜索群组");
        }
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    llFPanel.setVisibility(View.GONE);
                    llGPanel.setVisibility(View.GONE);
                } else {
                    if (searchType == Canstants.SEARCH_TYPE_FRIEND_GROUP) {
                        llFPanel.setVisibility(View.VISIBLE);
                        llGPanel.setVisibility(View.VISIBLE);
                    } else if (searchType == Canstants.SEARCH_TYPE_FRIEND) {
                        llFPanel.setVisibility(View.VISIBLE);
                        llGPanel.setVisibility(View.GONE);
                    } else if (searchType == Canstants.SEARCH_TYPE_GROUP) {
                        llFPanel.setVisibility(View.GONE);
                        llGPanel.setVisibility(View.VISIBLE);
                    }
                    tvFContent.setText(String.format("搜索好友\"%s\"", s));
                    tvGContent.setText(String.format("搜索群\"%s\"", s));
                }
            }
        });
    }

    public static void jumpTo(Context context, int searchType) {
        Intent intent = new Intent(context, SearchFriendActivity.class);
        intent.putExtra(Canstants.KEY_SEARCH_TYPE, searchType);
        context.startActivity(intent);
    }

    @OnClick({R.id.ll_f_panel, R.id.ll_g_panel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_f_panel:
                if (!TextUtils.isEmpty(etSearch.getText())) {
                    SearchListActivity.jumpTo(this, SearchListActivity.SEARCH_TYPE_FRIEND, etSearch.getText().toString());
                }
                break;
            case R.id.ll_g_panel:
                if (!TextUtils.isEmpty(etSearch.getText())) {
                    SearchListActivity.jumpTo(this, SearchListActivity.SEARCH_TYPE_GROUP, etSearch.getText().toString());
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Canstants.REQUEST_CODE_BACK && resultCode == Canstants.REQUEST_CODE_BACK) {
//            etSearch.setText(null);
            finish();
        }
    }
}
